package cn.fl.sue.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.sue.api.ISueApplyViewApiService;
import cn.fl.sue.constant.SueDicCode;
import cn.fl.sue.dto.SueApplyViewDTO;
import cn.fl.sue.dto.SueApplyViewDtlDTO;
import cn.fl.sue.dto.SueBillDTO;
import cn.fl.sue.dto.SueMcrBillDTO;
import cn.fl.sue.service.ISueApplyViewService;
import cn.fl.sue.vo.SueApplyViewDtlVO;
import cn.fl.sue.vo.SueApplyViewVO;
import cn.fl.sue.voTransform.SueApplyViewDtlVOTransform;
import cn.fl.sue.voTransform.SueApplyViewVOTransform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 作者:hongyanbo
 * 时间:2019-03-05
 */
@Service
public class SueApplyViewServiceImpl implements ISueApplyViewService{
    @Autowired
    private ISueApplyViewApiService sueApplyViewApiService;
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysOrgApiService sysOrgApiService;
    @Override
    public Pager selectListPage(Pager page) {
        Pager pager = sueApplyViewApiService.searchListPage(page);
        List<SueApplyViewVO> voList = setDic(pager);
        pager.setResultList(voList);
        return pager;
    }

    private List<SueApplyViewVO> setDic(Pager pager) {
        List<SueApplyViewDTO> resultList = (List<SueApplyViewDTO>) pager.getResultList();
        List<SueApplyViewVO> voList = SueApplyViewVOTransform.toVOList(resultList);
        if(CollectionUtils.isNotEmpty(voList)){
            // 融资租赁项目 业务模式
            Map<String, String> prjBizTypCdDic = sysDictionaryApiService.searchGroupToMap("11000", "0000", "PRJ0023");
            Map<String, String> mcrBizTypCdDic = sysDictionaryApiService.searchGroupToMap("11000", "0000", "MCR0007");
            Map<String, String> PrjTypCdDic = sysDictionaryApiService.searchGroupToMapAll("11000", "0000", "PRJ0002");
            Map<String, String> mcrPrjTypCdDic = sysDictionaryApiService.searchGroupToMap("11000", "0000", "MCR0001");
            Map<String, String> sueStsCdDic = sysDictionaryApiService.searchGroupToMap("11000", "0000", SueDicCode.SUE0007);
            // transfer dic
            for (SueApplyViewVO vo : voList) {
                String bizTypCd = vo.getBizTypCd();
                String bizTypCdDic = vo.getBizTypCdDic();
                if(StringUtils.isNotBlank(bizTypCd) && StringUtils.isNotBlank(bizTypCdDic)){
                    if("PRJ0023".equals(bizTypCdDic)){
                        vo.setBizTypCdNm(prjBizTypCdDic.get(bizTypCd));
                    }else if("MCR0007".equals(bizTypCdDic)){
                        vo.setBizTypCdNm(mcrBizTypCdDic.get(bizTypCd));
                    }
                }
                String prjTypCd = vo.getPrjTypCd();
                String prjTypCdDic = vo.getPrjTypCdDic();
                if(StringUtils.isNotBlank(prjTypCd) && StringUtils.isNotBlank(prjTypCdDic)){
                    if("PRJ0002".equals(prjTypCdDic)){
                        vo.setPrjTypCdNm(PrjTypCdDic.get(prjTypCd));
                    }else if("MCR0001".equals(prjTypCdDic)){
                        vo.setPrjTypCdNm(mcrPrjTypCdDic.get(prjTypCd));
                    }
                }
                String isSplMfr = vo.getIsSplMfr();
                if(StringUtils.isNotBlank(isSplMfr)){
                    if("1".equals(isSplMfr)){
                        vo.setIsSplMfr("是");
                    }else{
                        vo.setIsSplMfr("否");
                    }
                }
                String cstMgrOrgCd = vo.getCstMgrOrgCd();
                if(StringUtils.isNotBlank(cstMgrOrgCd)){
                    SysOrgDTO sysOrgDTO = sysOrgApiService.selectSysOrgByOrgCd(cstMgrOrgCd, "11000");
                    vo.setOrgDispNm(sysOrgDTO.getOrgDispNm());
                }
                String sueStsCd = vo.getSueStsCd();
                if(StringUtils.isNotBlank(sueStsCd)){
                    vo.setSueStsCd(sueStsCdDic.get(sueStsCd));
                }
            }
        }
        return voList;
    }

    @Override
    public Pager selectApplyListPage(Pager page) {
        Pager pager = sueApplyViewApiService.selectApplyListPage(page);
        List<SueApplyViewVO> voList = setDic(pager);
        pager.setResultList(voList);
        return pager;
    }

    @Override
    public List<SueApplyViewDtlVO> selectDtlList(Long dataId, String dataTable) {
        Map<String, String> PRJ0037 = this.sysDictionaryApiService.searchGroupToMap("11000", "0000", "PRJ0037");
        List<SueApplyViewDtlDTO> dtoList = this.sueApplyViewApiService.selectDtlList(dataId, dataTable);
        if(CollectionUtils.isNotEmpty(dtoList)){
            for (SueApplyViewDtlDTO dtlDTO : dtoList) {
                String prdStsCd = dtlDTO.getPrdStsCd();
                if(StringUtils.isNotBlank(prdStsCd)){
                    dtlDTO.setPrdStsCd(PRJ0037.get(prdStsCd));
                }
            }
        }
        return SueApplyViewDtlVOTransform.toVOList(dtoList);
    }

    @Override
    public List<SueBillDTO> selectBillList(Long dataId) {
        return sueApplyViewApiService.selectBillList(dataId);
    }

    @Override
    public SueMcrBillDTO selectMcrBill(Long mcrId) {
        return sueApplyViewApiService.selectMcrBill(mcrId);
    }
}
